//
//  TSTabbarController.m
//  TopTen
//
//  Created by 易金鑫 on 17/2/28.
//  Copyright © 2017年 Toocms. All rights reserved.
//

#import "TSTabbarController.h"

#import "TSNavigationController.h"
#import "YHHomepageVC.h"//首页
#import "YHTowViewController.h"//2
#import "YHThreeViewController.h"//3
#import "YHForeViewController.h" //4


#define DWColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define DWRandomColor DWColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

@interface TSTabbarController ()<UITabBarDelegate>

@end

@implementation TSTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置 TabBarItemTestAttributes 的颜色。
    [self setUpTabBarItemTextAttributes];
     [self initChildViewController];
   
    
    // 处理tabBar，使用自定义 tabBar 添加 发布按钮
//    [self setUpTabBar];
    
    [[UITabBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]]];
    
    //去除 TabBar 自带的顶部阴影
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

    UIView *lineView = [[UIView alloc] initWithFrame:Rect(0, 0, kScreenWidth, 1)];
    lineView.backgroundColor = HexColor(@"e1e1e1");
    [self.tabBar addSubview:lineView];
    self.delegate = self;
  
//    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:PresentPublishControllerNotification object:nil] subscribeNext:^(id x) {
//        
////        //先判断是否登录，没有登录跳到登录界面
////        if (![User userIsLogin]) {
////
////            LoginController *loginVC = [LoginController new];
////
////            loginVC.fromWhere = LoginPageFromTabBar;
////
////            TSNavigationController *nav = [[TSNavigationController alloc] initWithRootViewController:loginVC];
////
////            [self presentViewController:nav animated:YES completion:nil];
////
////            return;
////        }
//        
//        
//        TSHomePageVC *vc = [[TSHomePageVC alloc] init];
//        
//        TSNavigationController *nav = [[TSNavigationController alloc] initWithRootViewController:vc];
//        
//        [self presentViewController:nav animated:YES completion:nil];
//    }];
    
}

/**
 *  添加子控制器，我这里只添加了4个
 */
-(void)initChildViewController{
    
    TSNavigationController *nav1 = [[TSNavigationController alloc] initWithRootViewController:[YHHomepageVC alloc]];
    
    TSNavigationController *nav2 =[[TSNavigationController alloc] initWithRootViewController:[YHTowViewController alloc]];
    
    
    TSNavigationController *nav3 = [[TSNavigationController alloc] initWithRootViewController:[YHThreeViewController alloc]];
    
    TSNavigationController *nav4 = [[TSNavigationController alloc] initWithRootViewController:[YHForeViewController alloc]];



    [self addOneChildViewController:nav1 WithTitle:@"首页" imageName:@"main_tab_item_icon1_normal" selectedImageName:@"main_tab_item_icon1_checked"];
     [self addOneChildViewController:nav2 WithTitle:@"分类" imageName:@"main_tab_item_icon2_normal" selectedImageName:@"main_tab_item_icon2_checked"];
    [self addOneChildViewController:nav3 WithTitle:@"购物车" imageName:@"main_tab_item_icon3_normal" selectedImageName:@"main_tab_item_icon3_checked"];
    [self addOneChildViewController:nav4 WithTitle:@"我的" imageName:@"main_tab_item_icon4_normal" selectedImageName:@"main_tab_item_icon4_checked"];

}

/**
 *  添加一个子控制器
 *
 *  @param viewController    控制器
 *  @param title             标题
 *  @param imageName         图片
 *  @param selectedImageName 选中图片
 */

- (void)addOneChildViewController:(UIViewController *)viewController WithTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    
    viewController.view.backgroundColor     = [UIColor whiteColor];
    viewController.tabBarItem.title         = title;
    viewController.tabBarItem.image         = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *image = [UIImage imageNamed:selectedImageName];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = image;
    [self addChildViewController:viewController];

}

/**
 *  tabBarItem 的选中和不选中文字属性
 */
- (void)setUpTabBarItemTextAttributes{
    
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = HexColor(@"4c4c4c");
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = ThemeColor;
    
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
/**
 *  利用 KVC 把 系统的 tabBar 类型改为自定义类型。
 */
//- (void)setUpTabBar{
//
//    [self setValue:[[DWTabBar alloc] init] forKey:@"tabBar"];
//}
//这个方法可以抽取到 UIImage 的分类中
- (UIImage *)imageWithColor:(UIColor *)color
{
    NSParameterAssert(color != nil);
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // Create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);   // Fill it with your color
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


//禁止tab多次点击
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    UIViewController *selectVC = tabBarController.selectedViewController;
    
    if([selectVC isEqual:viewController]){
        
        return NO;
    }

//    if ([[viewController.childViewControllers[0] class] isEqual:[TSShoppingCartVC class]]) {
//
//        if (User_is_login) {
//
//        }else{
//            //登录界面
//            TSLoginVC *vc = [TSLoginVC new];
//            [(TSNavigationController *)selectVC pushViewController:vc animated:YES];
//            return NO;
//        }
//    }
    return YES;
}
#pragma mark - <UITabBarControllerDelegate>
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    //点击tabBarItem动画
    [self tabBarButtonClick:[self getTabBarButton]];
    
}
- (UIControl *)getTabBarButton{
    NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            [tabBarButtons addObject:tabBarButton];
        }
    }
    UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
    
    return tabBarButton;
}
#pragma mark - 点击动画
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要实现的帧动画,这里根据自己需求改动
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.5;
            animation.calculationMode = kCAAnimationCubic;
            //添加动画
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
